﻿
#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include<Windows.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
/*
*项目:c语言推箱子
*编译器：vs2019
*作者：xiaocheng
*/
int arr[3][10][10] =
{
	//11草10树9蓝色房子8黄色房子
	 {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 11, 8, 11, 1, 1, 1, 1, 8, 0,
		0, 11, 1, 1, 1, 0, 0, 1, 9, 0,
		0, 9, 1, 4, 3, 0, 0, 1, 10, 0,
		0, 11, 1, 0, 3, 3, 4, 1, 8, 0,
		0, 8, 1, 2, 3, 4, 1, 1, 11, 0,//2人物
		0, 10, 1, 1, 4, 1, 1, 1, 11, 0,//3箱子
		0, 0, 10, 1, 1, 1, 11, 11, 0, 0,//4目的地
		0, 0, 0, 11, 11, 11, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

	 {
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 1, 1, 1, 1, 1, 1, 1, 0, 0,
		0, 1, 0, 0, 0, 0, 0, 1, 1, 0,
		0, 1, 0, 3, 0, 0, 0, 0, 1, 0,
		0, 1, 2, 3, 1, 1, 3, 0, 1, 0,
		0, 1, 3, 3, 4, 1, 0, 0, 1, 0,//2人物
		0, 1, 4, 4, 4, 0, 4, 1, 1, 0,//3箱子
		0, 1, 1, 1, 1, 1, 1, 1, 0, 0,//4目的地
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0},


	 {  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
		0, 0, 1, 1, 0, 0, 0, 1, 1, 0,
		0, 0, 1, 0, 0, 3, 4, 4, 1, 1,
		0, 1, 1, 3, 0, 1, 4, 3, 4, 1,
		0, 1, 0, 0, 0, 1, 4, 4, 4, 1,
		0, 1, 0, 3, 1, 1, 1, 3, 0, 1,
		0, 1, 0, 0, 3, 0, 3, 0, 1, 1,
		0, 1, 1, 2, 0, 0, 0, 0, 1, 0,
		0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
	 }
};
IMAGE img[20];//图片数组
int life = 0;
int map[10][10];
int level = 0;//关卡等级
void init()
{
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			map[i][j] = arr[level][i][j];
		}
	}
}
void DrawMap()
{
	BeginBatchDraw();
	putimage(0, 0, &img[10]);//背景
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			switch (map[i][j])
			{
			case 1:
				putimage(50 * j, 50 * i, &img[0]);//围墙
				break;
			case 2:
				putimage(50 * j, 50 * i, &img[2]);//人物
				break;
			case 3:
				putimage(50 * j, 50 * i, &img[12]);//箱子
				break;
			case 4:
				putimage(50 * j, 50 * i, &img[9]);//目的地
				break;
			case 6:
				putimage(50 * j, 50 * i, &img[2]);//人在目的地
				break;
			case 7:
				putimage(50 * j, 50 * i, &img[11]); //箱子在目的地
				break;
			case 8:
				putimage(50 * j, 50 * i, &img[6]); //黄色房子
				break;
			case 9:
				putimage(50 * j, 50 * i, &img[7]); //蓝色房子
				break;
			case 10:
				putimage(50 * j, 50 * i, &img[8]); //树
				break;
			case 11:
				putimage(50 * j, 50 * i, &img[5]); //草
				break;
			}
		}
	}EndBatchDraw();
}
void piay()
{
	int x, y;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[i][j] == 2 || map[i][j] == 6)
			{
				x = i;
				y = j;
			}
		}
	}
	switch (_getch())
	{
	case 72:
		if (map[x - 1][y] == 0 || map[x - 1][y] == 4)
		{
			map[x - 1][y] += 2;
			map[x][y] -= 2;
		}
		else if (map[x - 1][y] == 3 || map[x - 1][y] == 7)
		{
			if (map[x - 2][y] == 0 || map[x - 2][y] == 4)
			{
				map[x][y] -= 2;
				map[x - 1][y] -= 1;
				map[x - 2][y] += 3;
			}
		}
		break;
	case 80:if (map[x + 1][y] == 0 || map[x + 1][y] == 4)
	{
		map[x + 1][y] += 2;
		map[x][y] -= 2;
	}
    else if (map[x + 1][y] == 3 || map[x + 1][y] == 7)
	{
		if (map[x + 2][y] == 0 || map[x + 2][y] == 4)
		{
			map[x][y] -= 2;
			map[x + 1][y] -= 1;
			map[x + 2][y] += 3;
		}
	}
		   break;
	case 75:
		if (map[x][y - 1] == 0 || map[x][y - 1] == 4)
		{
			map[x][y - 1] += 2;
			map[x][y] -= 2;
		}
		else if (map[x][y - 1] == 3 || map[x][y - 1] == 7)
		{
			if (map[x][y - 2] == 0 || map[x][y - 2] == 4)
			{
				map[x][y] -= 2;
				map[x][y - 1] -= 1;
				map[x][y - 2] += 3;
			}
		}
		break;
	case 77:
		if (map[x][y + 1] == 0 || map[x][y + 1] == 4)
		{
			map[x][y + 1] += 2;
			map[x][y] -= 2;
		}
		else if (map[x][y + 1] == 3 || map[x][y + 1] == 7)
		{
			if (map[x][y + 2] == 0 || map[x][y + 2] == 4)
			{
				map[x][y] -= 2;
				map[x][y + 1] -= 1;
				map[x][y + 2] += 3;
			}
		}
		break;
	case 'w':
		life = 2;
		break;
	}

}
void Drawing()//画图
{
	setfillcolor(LIGHTGREEN);//绿色
	fillrectangle(700, 0, 500, 17);
	setfillcolor(LIGHTMAGENTA);//粉红
	fillrectangle(700, 17, 500, 37);
	setfillcolor(RED);//红色
	fillrectangle(700, 37, 500, 57);
	setfillcolor(BLUE);// 蓝色
	fillrectangle(700, 57, 500, 77);
	setfillcolor(MAGENTA);//紫色
	fillrectangle(700, 77, 500, 97);
	setfillcolor(BROWN);//棕色
	fillrectangle(700, 97, 500, 117);
	setfillcolor(DARKGRAY);//灰色
	fillrectangle(700, 117, 500, 137);
	setfillcolor(LIGHTCYAN);//亮青
	fillrectangle(700, 137, 500, 157);
	setfillcolor(LIGHTRED);//亮红
	fillrectangle(700, 157, 500, 177);
	setfillcolor(YELLOW);//黄色
	fillrectangle(700, 177, 500, 197);
	setfillcolor(WHITE);//白色
	fillrectangle(700, 197, 500, 217);
	setfillcolor(LIGHTGREEN);//亮绿
	fillrectangle(700, 217, 500, 237);
	setfillcolor(CYAN);//青
	fillrectangle(700, 237, 500, 257);
	setfillcolor(LIGHTRED);
	fillrectangle(700, 257, 500, 500);
	fillcircle(600, 370, 1);
	setfillcolor(LIGHTGREEN);
	fillcircle(600, 370, 100);
	setfillcolor(WHITE);
	fillcircle(600, 370, 90);
	setfillcolor(LIGHTCYAN);
	fillcircle(600, 370, 80);
	setfillcolor(LIGHTMAGENTA);
	fillcircle(600, 370, 70);
	setfillcolor(YELLOW);
	fillcircle(600, 370, 60);
	setfillcolor(RED);
	fillcircle(600, 370, 50);
	setfillcolor(MAGENTA);
	fillcircle(600, 370, 40);
	setfillcolor(CYAN);
	fillcircle(600, 370, 30);
	setfillcolor(BLUE);
	fillcircle(600, 370, 20);
	setfillcolor(BROWN);
	fillcircle(600, 370, 10);
	setfillcolor(BLACK);
	fillcircle(600, 370, 3);
}
void Music()//判断音乐
{
	switch (level)
	{
	case 0:
		mciSendString("close 音乐/4.mp3", 0, 0, 0);
		mciSendString("open 音乐/1.mp3", 0, 0, 0);
		mciSendString("play 音乐/1.mp3 ", 0, 0, 0);
		break;
	case 1:
		mciSendString("close 音乐/1.mp3", 0, 0, 0);
		mciSendString("open 音乐/2.mp3", 0, 0, 0);
		mciSendString("play 音乐/2.mp3 ", 0, 0, 0);
		break;
	case 2:
		mciSendString("close 音乐/2.mp3", 0, 0, 0);
		mciSendString("open 音乐/3.mp3", 0, 0, 0);
		mciSendString("play 音乐/3.mp3 ", 0, 0, 0);
		break;
	}
}
void win()
{
	int number = 0;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[i][j] == 3)
			{
				number++;
			}
		}
	}
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (map[i][j] == 3)
			{
				if (map[i - 1][j] == 1 || map[i + 1][j] == 1)
				{
					if (map[i][j - 1] == 1 || map[i][j + 1] == 1)
					{
						life = 2;
					}
				}
			}
		}
	}
	if (number == 0)
	{
		life = 1;
	}
}
void Description()//说明
{
	settextcolor(YELLOW);
	outtextxy(515, 0, "程序名称：推箱子小游戏");
	settextcolor(LIGHTCYAN);
	outtextxy(530, 20, "程序编写：Xiaochen");
	settextcolor(LIGHTMAGENTA);
	outtextxy(520, 40, "编写者QQ：3165129969");
	settextcolor(LIGHTGREEN);
	outtextxy(570, 60, "操作说明");
	settextcolor(WHITE);
	outtextxy(595, 80, "上");
	outtextxy(570, 100, "左");
	outtextxy(595, 100, "下");
	outtextxy(620, 100, "右");
	settextcolor(YELLOW);
	outtextxy(550, 120, "重新本关请按w");

}

void picture()
{
	loadimage(&img[0], "图片/围墙.jpg", 50, 50);
	loadimage(&img[1], "图片/上.jpg", 50, 50);
	loadimage(&img[2], "图片/下.jpg", 50, 50);
	loadimage(&img[3], "图片/左.jpg", 50, 50);
	loadimage(&img[4], "图片/右.jpg", 50, 50);
	loadimage(&img[5], "图片/草.jpg", 50, 50);
	loadimage(&img[6], "图片/黄色房子.jpg", 50, 50);
	loadimage(&img[7], "图片/蓝色房子.jpg", 50, 50);
	loadimage(&img[8], "图片/树.jpg", 50, 50);
	loadimage(&img[9], "图片/目的地.jpg", 50, 50);
	loadimage(&img[10], "图片/背景.jpg", 500, 500);
	loadimage(&img[11], "图片/箱子在目的地.jpg", 50, 50);
	loadimage(&img[12], "图片/箱子.jpg", 50, 50);
	loadimage(&img[13], "图片/胜利.jpg", 500, 500);
	loadimage(&img[14], "图片/失败.jpg", 500, 500);
}
int main()
{
	/*loadimage(&img[0], "图片/围墙.jpg", 50, 50);
	loadimage(&img[1], "图片/上.jpg", 50, 50);
	loadimage(&img[2], "图片/下.jpg", 50, 50);
	loadimage(&img[3], "图片/左.jpg", 50, 50);
	loadimage(&img[4], "图片/右.jpg", 50, 50);
	loadimage(&img[5], "图片/草.jpg", 50, 50);
	loadimage(&img[6], "图片/黄色房子.jpg", 50, 50);
	loadimage(&img[7], "图片/蓝色房子.jpg", 50, 50);
	loadimage(&img[8], "图片/树.jpg", 50, 50);
	loadimage(&img[9], "图片/目的地.jpg", 50, 50);
	loadimage(&img[10], "图片/背景.jpg", 500, 500);
	loadimage(&img[11], "图片/箱子在目的地.jpg", 50, 50);
	loadimage(&img[12], "图片/箱子.jpg", 50, 50);
	loadimage(&img[13], "图片/胜利.jpg", 500, 500);
	loadimage(&img[14], "图片/失败.jpg", 500, 500);*/
	picture();
	initgraph(700, 500);//窗口大小
	init();//数组转换
	DrawMap();//第一次贴图
		while (1)
		{
			Drawing();//画图
			Description();//操作说明
			Music();//音乐
			piay();//操作
			DrawMap();//贴图
			win();//判断游戏是否撞墙
			if (life == 1)
			{
				if (MessageBox(GetHWnd(), "游戏胜利", "继续下一关", MB_YESNO) == IDYES)
				{
					level++;
					if (level == 3)
					{
						putimage(0, 0, &img[6]);
						mciSendString("close 音乐/3.mp3", 0, 0, 0);
						mciSendString("open 音乐/4.mp3", 0, 0, 0);
						mciSendString("play 音乐/4.mp3 ", 0, 0, 0);
						if (MessageBox(GetHWnd(), "最后一关", "是否重新游戏", MB_YESNO) == IDYES)
						{
							level = 0;
							init();
							DrawMap();
							life = 0;
						}
						else return 0;
					}
					init();
					DrawMap();
					life = 0;
				}
				else return 0;
			}
			if (life == 2)
			{
				putimage(0, 0, &img[7]);
				mciSendString("close 音乐/1.mp3", 0, 0, 0);
				mciSendString("close 音乐/2.mp3", 0, 0, 0);
				mciSendString("close 音乐/3.mp3", 0, 0, 0);
				mciSendString("play 音乐/5.mp3 ", 0, 0, 0);
				if (MessageBox(GetHWnd(), "游戏失败", "重新开始游戏", MB_YESNO) == IDYES)
				{
					life = 0;
					init();
					DrawMap();
				}
				else return 0;
			}
		}
	closegraph();
	return 0;
}